package vn.com.shoppie.activity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.message.BasicNameValuePair;

import vn.com.shoppie.network.WiModelManager;
import vn.com.shoppie.sobject.ShoppieObject;
import vn.com.shoppie.sobject.User;
import vn.com.shoppie.util.SConfig;
import vn.com.shoppie.util.SUtil;
import vn.com.shoppie.util.SUtilXml;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class BluetoothExample extends FragmentActivity {
	private static final int REQUEST_ENABLE_BT = 0;
	private static final int REQUEST_DISCOVERABLE_BT = 1;
	BluetoothAdapter mBluetoothAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_bluetooth);
		final TextView out = (TextView) findViewById(R.id.out);
		final Button button = (Button) findViewById(R.id.button1);
		final Button button1 = (Button) findViewById(R.id.button2);
		final Button button2 = (Button) findViewById(R.id.button3);
		mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		if (mBluetoothAdapter == null) {
			out.append("device not supported");
		}
		button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				turnOnBluetooth();
			}
		});
		button1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				turnOnDiscoverBluetooth();
			}
		});
		button2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {

				registerAccount();
				turnOffBluetooth();

			}
		});

		out.setText("emei: " + SUtil.getInstance().getDeviceId(this) + " name:"
				+ SUtil.getInstance().getBluetoothName() + " address:"
				+ SUtil.getInstance().getBluetoothAddress());

	}
	
	public void turnOnBluetooth() {
		if (!mBluetoothAdapter.isEnabled()) {
			Intent enableBtIntent = new Intent(
					BluetoothAdapter.ACTION_REQUEST_ENABLE);
			startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
		}
	}

	public void turnOnDiscoverBluetooth() {
		if (!mBluetoothAdapter.isDiscovering()) {
			// out.append("MAKING YOUR DEVICE DISCOVERABLE");
			Context context = getApplicationContext();
			CharSequence text = "MAKING YOUR DEVICE DISCOVERABLE";
			int duration = Toast.LENGTH_SHORT;

			Toast toast = Toast.makeText(context, text, duration);
			toast.show();
			Intent enableBtIntent = new Intent(
					BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
			startActivityForResult(enableBtIntent, REQUEST_DISCOVERABLE_BT);
		}
	}

	public void registerAccount() {
		// send POST
		SUtil util = SUtil.getInstance();
		String url = SConfig.URL_SHOPPIE_HOME+SConfig.URL_REGISTER;
		String address = util.getBluetoothAddress();
		if (address.equals(null)) {
			Log.e("address", "null");
			return;
		}
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair("deviceToken", util
				.getDeviceId(BluetoothExample.this)));
		nameValuePairs.add(new BasicNameValuePair("deviceType", "Android"));
		nameValuePairs.add(new BasicNameValuePair("custCode", address));
		try {
			String xml = SUtilXml.getInstance().postXml(BluetoothExample.this,
					url, nameValuePairs, 200);
			WiModelManager mng = new WiModelManager();
			ArrayList<ShoppieObject> data = new ArrayList<ShoppieObject>();
			mng.parse(xml);
			data = mng.getResultShoppieObject();
			System.out.println(data.size() + "");
			for (ShoppieObject user : data) {
				System.out.println(((User) user).custId);
			}
			// System.out.println(xml);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void turnOffBluetooth() {
		mBluetoothAdapter.disable();
		// out.append("TURN_OFF BLUETOOTH");
		Context context = getApplicationContext();
		CharSequence text = "TURNING_OFF BLUETOOTH";
		Toast toast = Toast.makeText(context, text, 15);
		toast.show();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case REQUEST_DISCOVERABLE_BT:
			String output = "";
			for (BluetoothDevice devide : SUtil.getInstance().getListDevices()) {
				output += devide.getName() + "-" + devide.getAddress() + "-"
						+ devide.getBondState() + "\n";
			}
			((TextView) findViewById(R.id.out)).setText(output);
			break;
		case REQUEST_ENABLE_BT:
			((TextView) findViewById(R.id.out)).setText(mBluetoothAdapter
					.isEnabled() + "");
			break;
		default:
			break;
		}
	}
}